﻿using UnityEngine;
using System.Collections;

public class Weapon : Toucher 
{
    public float fireDelay = 1;
    public float startSpeed = 20;

    public Rigidbody2D rocket;
    public Rigidbody2D motor;

    public Transform firePos;

    float lastFireTime = Time.time;
    
    Vector3 firstPosition;

    protected override void Start()
    {
        base.Start();
        rocket.isKinematic = true;
        firstPosition = transform.localPosition;
    }

    protected override void OnTouch()
    {
        Fire();
    }

    void FixedUpdate()
    {
        transform.localPosition = firstPosition;
    }

    public void Fire()
    {
        if (Time.time - lastFireTime > fireDelay)
        {
            lastFireTime = Time.time;      
            Vector2 direction = transform.right * startSpeed;

            Rigidbody2D bulletInstance = Instantiate(rocket, firePos.position, transform.rotation) as Rigidbody2D;
            bulletInstance.isKinematic = false;
            bulletInstance.velocity = direction;

            motor.velocity += -direction / 5;
        }
    }
}
